package com.ctshk.rpc.goods.mapper;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.goods.dto.CommodityClassificationAllLevelListDTO;
import com.ctshk.rpc.goods.dto.CommodityClassificationDTO;
import com.ctshk.rpc.goods.dto.CommodityClassificationLevelTwoDTO;
import com.ctshk.rpc.goods.dto.htmlFive.HtmlFiveCommodityClassificationAllLevelListDTO;
import com.ctshk.rpc.goods.dto.htmlFive.HtmlFiveCommodityClassificationLevelTwoDTO;
import com.ctshk.rpc.goods.entity.CommodityClassification;
import com.ctshk.rpc.goods.mapper.provider.CommodityClassificationProvider;
import com.ctshk.rpc.goods.req.CommodityClassificationPageReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 商品分类 Mapper 接口
 * </p>
 *
 * @author 姜斌文
 * @since 2021-04-08
 */
@SuppressWarnings("all")
public interface CommodityClassificationMapper extends BaseMapper<CommodityClassification> {
	// ==============================  中旅到家-后台 Start ==============================
	@Select("<script>"+"SELECT t.id,t.classification_name FROM `ctshk-goods`.t_commodity_classification t WHERE 1 = 1 AND t.is_deleted = 0 \n" +
			"<if test=\"level != null\"> AND t.classification_level = #{level}</if> \n" +
			"<if test=\"parentId != null\"> AND t.parent_id = #{parentId}</if> ORDER BY gmt_create desc"+"</script>")
	public List<CommodityClassificationDTO> findCommodityClassificationByLevel(@Param("level")Integer level, @Param("parentId")Long parentId);

	@SelectProvider(type = CommodityClassificationProvider.class, method = "findCommodityClassificationByCondition")
	IPage<CommodityClassificationDTO> findCommodityClassificationByCondition(IPage<CommodityClassificationDTO> page, CommodityClassificationPageReq pageReq);

	@Select("<script>" +
			"SELECT " +
			"	t.id, t.classification_name, t.classification_level " + "" +
			"FROM " +
			"	t_commodity_classification t " +
			"WHERE " +
			"	t.is_deleted = 0 AND t.classification_level = 1 <if test='userIds != null'>AND t.create_id in (${userIds})</if> ORDER BY t.sort ASC" +
			"</script>")
	List<CommodityClassificationDTO> findOneLeveClassificationNameList(@Param("userIds") String userIds);

	@Select({
			"<script>",
			"SELECT t.id, t.sort, t.classification_name, t.classification_level, t.parent_id ",
			"FROM t_commodity_classification t WHERE t.classification_level = 1 AND t.is_deleted = 0",
			"<if test='now != null'> AND t.gmt_modified >= #{now}</if> ",
			"<if test='userIds != null'> AND t.create_id in (${userIds})</if> ",
			"</script>"
	})
	List<CommodityClassificationAllLevelListDTO> findClassificationByLevelOne(@Param("now") LocalDateTime now, @Param("userIds") String userIds);

	@Select({
			"<script>",
			"SELECT t.id, t.sort, t.classification_name, t.classification_level, t.parent_id ",
			"FROM t_commodity_classification t WHERE t.classification_level = 2 AND t.is_deleted = 0 ",
			"<if test='now != null'> AND t.gmt_modified >= #{now}</if> ",
			"</script>"
	})
	List<CommodityClassificationLevelTwoDTO> findClassificationByLevelTwo(@Param("now") LocalDateTime now);
	// ==============================  中旅到家-后台 End ==============================



	// ==============================  中旅到家-H5端 Start ==============================

	/**
	 * 获取商品分类一级标签，
	 * @param now
	 * @return
	 */
	@Select({
			"<script>",
			"SELECT t.id, t.sort, t.classification_name, t.classification_level, t.parent_id ",
			"FROM t_commodity_classification t WHERE t.classification_level = 1 AND t.is_deleted = 0",
			"<if test='now != null'> AND t.gmt_modified >= #{now}</if> ",
			"</script>"
	})
	List<HtmlFiveCommodityClassificationAllLevelListDTO> findHFiveClassificationByLevelOne(@Param("now") LocalDateTime now);

	@Select({
			"<script>",
			"SELECT t.id, t.sort, t.classification_name, t.classification_level, t.parent_id ",
			"FROM t_commodity_classification t WHERE t.classification_level = 2 AND t.is_deleted = 0 ",
			"<if test='now != null'> AND t.gmt_modified >= #{now}</if> ",
			"</script>"
	})
	List<HtmlFiveCommodityClassificationLevelTwoDTO> findHFiveClassificationByLevelTwo(@Param("now") LocalDateTime now);
	// ==============================  中旅到家-H5端 End ==============================

	@Update("<script>" +
			" UPDATE t_commodity_classification t set" +
			" <if test='status != null'>t.status = #{status}</if> " +
			" <if test='isDisplay != null'>t.is_display = #{isDisplay}</if> " +
			" WHERE t.id = #{id} " +
			"</script>")
	Integer updateClassfincationStatus(@Param("id") Long id, @Param("status") Integer status, @Param("isDisplay") Integer isDisplay);

	@Select("<script>" +
			" SELECT DISTINCT " +
			" t.id AS id, " +
			" t.classification_name AS name " +
			"FROM " +
			" t_commodity, " +
			" t_commodity_classification t " +
			"WHERE " +
			" t_commodity.commodity_classification_id = t.id " +
			" <if test='userIds != null'>AND t.create_id in (${userIds})</if> " +
			"AND " +
			" t_commodity.is_deleted = 0 " +
			"ORDER BY t_commodity.sort DESC " +
			"</script>")
	List<JSONObject> findCommodityClassficationListAll(@Param("userIds") String userIds);
}
